package com.zzyl.nursing.service.impl;


import java.time.LocalDate;
import java.time.LocalTime;
import java.util.List;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zzyl.common.exception.base.BaseException;
import com.zzyl.common.utils.SecurityUtils;
import com.zzyl.common.utils.StringUtils;
import com.zzyl.nursing.domain.Device;
import com.zzyl.nursing.domain.Elder;
import com.zzyl.nursing.domain.NursingTask;
import com.zzyl.nursing.dto.AlertDataDto;
import com.zzyl.nursing.mapper.ElderMapper;
import com.zzyl.nursing.service.IDeviceService;
import com.zzyl.nursing.service.IElderService;
import com.zzyl.nursing.vo.AlertDataVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.AlertDataMapper;
import com.zzyl.nursing.domain.AlertData;
import com.zzyl.nursing.service.IAlertDataService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Arrays;

/**
 * 报警数据Service业务层处理
 *
 * @author syl
 * @date 2025-06-05
 */
@Service
public class AlertDataServiceImpl extends ServiceImpl<AlertDataMapper, AlertData> implements IAlertDataService {
    @Autowired
    private AlertDataMapper alertDataMapper;

    @Autowired
    private IDeviceService deviceService;


    /**
     * 查询报警数据
     *
     * @param id 报警数据主键
     * @return 报警数据
     */
    @Override
    public AlertData selectAlertDataById(Long id) {
        return getById(id);
    }

    /**
     * 查询报警数据列表
     *
     * @param alertDataVo 报警数据
     * @return 报警数据
     */
    @Override
    public List<AlertData> selectAlertDataList(AlertDataVo alertDataVo) {

        //获取护理人员Id
        Long careUserId = SecurityUtils.getUserId();
        //判断是否是该护理人员的老人
        //基础查询条件
        LambdaQueryWrapper<AlertData> queryWrapper = Wrappers.lambdaQuery();
        if (careUserId != 1) {
            queryWrapper.eq(AlertData::getUserId, careUserId);
            //动态查询条件
            // 设备名模糊查询
            if (StrUtil.isNotBlank(alertDataVo.getDeviceName())) {
                queryWrapper.like(AlertData::getDeviceName, alertDataVo.getDeviceName());
            }

            // 状态精确查询
            // 状态精确查询
            if (alertDataVo.getStatus() != null) {
                queryWrapper.eq(AlertData::getStatus, alertDataVo.getStatus());
            }

            // 创建时间范围查询
            if (alertDataVo.getStartTime() != null && alertDataVo.getEndTime() != null) {
                queryWrapper.between(AlertData::getCreateTime, alertDataVo.getStartTime(), alertDataVo.getEndTime());
            }
            return alertDataMapper.selectList(queryWrapper);
        }
        //动态查询条件
        // 设备名模糊查询
        if (StrUtil.isNotBlank(alertDataVo.getDeviceName())) {
            queryWrapper.like(AlertData::getDeviceName, alertDataVo.getDeviceName());
        }

        // 状态精确查询
        // 状态精确查询
        if (alertDataVo.getStatus() != null) {
            queryWrapper.eq(AlertData::getStatus, alertDataVo.getStatus());
        }

        // 创建时间范围查询
        if (alertDataVo.getStartTime() != null && alertDataVo.getEndTime() != null) {
            queryWrapper.between(AlertData::getCreateTime, alertDataVo.getStartTime(), alertDataVo.getEndTime());
        }


        queryWrapper.orderByDesc(AlertData::getCreateTime);
        List<AlertData> alertData = alertDataMapper.selectList(queryWrapper);
        alertData.forEach(v -> {
            String iotId = v.getIotId();
            LambdaQueryWrapper<Device> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.eq(Device::getIotId,iotId);
            Device one = deviceService.getOne(queryWrapper1);
            v.setRemark(one.getRemark());
        });

        return alertData;
    }

    /**
     * 新增报警数据
     *
     * @param alertData 报警数据
     * @return 结果
     */
    @Override
    public int insertAlertData(AlertData alertData) {
        return save(alertData) ? 1 : 0;
    }

    /**
     * 修改报警数据
     *
     * @param alertDataDto 报警数据
     * @return 结果
     */
    @Override
    public int updateAlertData(AlertDataDto alertDataDto) {
        if (alertDataDto == null) {
            throw new BaseException("报警数据不存在");
        }
        AlertData alertData = new AlertData();
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();

        //获取老人信息
        alertData.setId(alertDataDto.getId());
        alertData.setIotId(alertDataDto.getIotId());
        alertData.setDeviceName(alertDataDto.getDeviceName());
        alertData.setProductKey(alertDataDto.getProductKey());
        alertData.setProductName(alertDataDto.getProductName());
        alertData.setFunctionId(alertDataDto.getFunctionId());
        alertData.setAccessLocation(alertDataDto.getAccessLocation());
        alertData.setLocationType(alertDataDto.getLocationType());
        alertData.setPhysicalLocationType(alertDataDto.getPhysicalLocationType());
        alertData.setDeviceDescription(alertDataDto.getDeviceDescription());
        alertData.setDataValue(alertDataDto.getDataValue());
        alertData.setAlertRuleId(alertDataDto.getAlertRuleId());
        alertData.setAlertReason(alertDataDto.getAlertReason());
        alertData.setProcessorId(SecurityUtils.getUserId());
        alertData.setProcessorName(authentication.getName());
        alertData.setProcessingResult(alertDataDto.getProcessingResult());
        alertData.setProcessingTime(alertDataDto.getProcessingTime());
        alertData.setType(alertDataDto.getType());
        alertData.setStatus(1);
        alertData.setUserId(alertDataDto.getUserId());

        return alertDataMapper.updateAlertData(alertData);
    }

    /**
     * 批量删除报警数据
     *
     * @param ids 需要删除的报警数据主键
     * @return 结果
     */
    @Override
    public int deleteAlertDataByIds(Long[] ids) {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除报警数据信息
     *
     * @param id 报警数据主键
     * @return 结果
     */
    @Override
    public int deleteAlertDataById(Long id) {
        return removeById(id) ? 1 : 0;
    }


    /*@Override
    public int handleAlertData(Long id, String processingResult, Long userId, String userName) {
        AlertData alertData = new AlertData();
        if (alertData == null) {
            throw new BaseException("报警数据不存在");
        }
        //更新报警数据
        LambdaUpdateWrapper<AlertData> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(AlertData::getId, id);
        updateWrapper.set(AlertData::getProcessingResult, processingResult);
        return 1;

    }*/
}
